﻿//------------------------------------------------------------------------------
// @Class name: EmailLib
// @Description: Library email is used to send mail on website 
// @Author: Nguyen Thanh Hoang
// @History: 
//  - Created: 2013/07/22
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace EmailLib
{
    public class Email
    {
        public string Username { get; set; }
        public string Password { get; set; }
        public string HostMailServer { get; set; }
        public int PortMailServer { get; set; }
        public int Timeout { get; set; }

        public string ToAddress { get; set; }
        public string CcMail { get; set; }
        public string BccMail { get; set; }
        public string SubjectMail { get; set; }
        public string BodyMail { get; set; }
        
        private SmtpClient smtp;
        private bool IsConfigured;          //flag to show mail configured

        //------------------------------------------------------------------------------
        // @Function name: Contructor of email
        // @Description: Initinal email with smtp of google
        // @Parameters: None
        // @Note:
        // - Host default: smtp.gmail.com
        // - Port default: 587
        // - Timeout: 0ms
        //------------------------------------------------------------------------------
        public Email()
        {
            Username = "";
            Password = "";
            HostMailServer = "";
            PortMailServer = 0;
            Timeout = 0;

            IsConfigured = false;
        }

        //------------------------------------------------------------------------------
        // @Function name: Contructor of email
        // @Description: Initinal email with smtp of google
        // @Parameters: None
        // @Note:
        // - Host default: smtp.gmail.com
        // - Port default: 587
        // - Timeout: 0ms
        //------------------------------------------------------------------------------
        public Email(string strUsername, string strPassword)
        {
            string strHostMail = "smtp.gmail.com";
            int nPort = 587;

            //smtp settings
            smtp = new SmtpClient();
            ConfigureMail(strUsername, strPassword, strHostMail, nPort);
        }

        //------------------------------------------------------------------------------
        // @Function name: Contructor of email
        // @Description: Initinal email with optional settings
        // @Parameters: 
        //  - Hostname  : string
        //  - Port      : int
        //  - Timeout   : int
        //------------------------------------------------------------------------------
        public Email(string strUsername, string strPassword, string strHostMail = "smtp.gmail.com", int nPortMail = 587, int nTimeout = 0)
        {
            smtp = new SmtpClient();
            ConfigureMail(strUsername, strPassword, strHostMail, nPortMail, nTimeout);
        }

        //------------------------------------------------------------------------------
        // @Function name: ConfigureMail
        // @Description: Configure mail with username, password, HostMail, PortMail
        // @Parameters: 
        //  - Hostname  : string
        //  - Port      : int
        //  - Timeout   : int
        //------------------------------------------------------------------------------
        public bool ConfigureMail(string strUsername, string strPassword, string strHostname, int nPort, int nTimeout = 0)
        {
            Username = strUsername;
            Password = strPassword;
            HostMailServer = strHostname;
            PortMailServer = nPort;
            Timeout = nTimeout;

            try
            { 
                 //setting smtp
                smtp.Host = HostMailServer;
                smtp.Port = PortMailServer;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Credentials = new System.Net.NetworkCredential(Username, Password);
                smtp.Timeout = Timeout;

                IsConfigured = true;
            }
            catch
            {
                IsConfigured = false;
            }

            return IsConfigured;
           
        }

        //------------------------------------------------------------------------------
        // @Function name: SendMail()
        // @Description: send mail with settings default
        // @Return: 
        //  - True : Send mail successfully.
        //  - False: Send mail fail 
        //------------------------------------------------------------------------------
        public bool SendMail(string strUsername, string strPassword, string strToAddress, string strSubjectMail, string strBodyMail)
        {
            try
            {
                if (IsConfigured && IsValidateMailAddress(strToAddress))
                {
                    smtp.Credentials = new System.Net.NetworkCredential(strUsername, strPassword);
                    smtp.Send(strUsername, strToAddress, strSubjectMail, strBodyMail);

                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        //------------------------------------------------------------------------------
        // @Function name: SendMail()
        // @Description: send mail with setting default
        // @Return: 
        //  - True : Send mail successfully.
        //  - False: Send mail fail 
        //------------------------------------------------------------------------------
        public bool SendMail(string toAddress, string subject, string body)
        {
            try
            {
                if (IsConfigured && IsValidateMailAddress(toAddress))
                {
                    smtp.Send(Username, toAddress, subject, body);

                    return true;
                }
                else
                {
                    return false;
                }
                    
            }
            catch (Exception)
            {
                return false;
            }
        }
        //------------------------------------------------------------------------------
        // @Function name: IsValidateMail()
        // @Description: send mail with setting default
        // @Return: 
        //  - True : Send mail successfully.
        //  - False: Send mail fail 
        //------------------------------------------------------------------------------
        public bool IsValidateMailAddress(string strEmail)
        {
            bool bResult = false;
            bResult = Regex.IsMatch(strEmail, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

            return bResult;
        }

    }
}
